package com.lagou.sqlSession;


import com.lagou.config.BoundSql;
import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import com.lagou.utils.GenericTokenParser;
import com.lagou.utils.ParameterMapping;
import com.lagou.utils.ParameterMappingTokenHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class SimpleExecutor implements Executor {
    @Override
    public <E> List<E> query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        Connection connection = configuration.getDataSource().getConnection();
        PreparedStatement preparedStatement = getPreparedStatement(connection, configuration, mappedStatement, params);
        TypeHandler typeHandler = configuration.getTypeHandler();
        ResultSet resultSet = preparedStatement.executeQuery();
        ResultSetHandler resultSetHandler = configuration.getResultSetHandler();
        return resultSetHandler.handleResultSets(typeHandler, mappedStatement, resultSet);

    }

    @Override
    public long insert(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        return update(configuration, mappedStatement, params);
    }

    @Override
    public long delete(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        return update(configuration, mappedStatement, params);
    }

    @Override
    public long update(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        Connection connection = configuration.getDataSource().getConnection();
        PreparedStatement preparedStatement = getPreparedStatement(connection, configuration, mappedStatement, params);
        try {
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            connection.close();
            preparedStatement.close();
        }
    }

    private PreparedStatement getPreparedStatement(Connection connection, Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {
        String sql = mappedStatement.getSql();
        BoundSql boundSql = getBoundSql(sql);
        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText());

        ParameterHandler parameterHandler = configuration.getParameterHandler();
        TypeHandler typeHandler = configuration.getTypeHandler();
        parameterHandler.setParameters(typeHandler, boundSql, preparedStatement, mappedStatement, params);
        return preparedStatement;
    }


    /**
     * 完成对#{}的解析工作：1.将#{}使用？进行代替，2.解析出#{}里面的值进行存储
     *
     * @param sql
     * @return
     */
    private BoundSql getBoundSql(String sql) {
        //标记处理类：配置标记解析器来完成对占位符的解析处理工作
        ParameterMappingTokenHandler parameterMappingTokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser genericTokenParser = new GenericTokenParser("#{", "}", parameterMappingTokenHandler);
        //解析出来的sql
        String parseSql = genericTokenParser.parse(sql);
        //#{}里面解析出来的参数名称
        List<ParameterMapping> parameterMappings = parameterMappingTokenHandler.getParameterMappings();

        BoundSql boundSql = new BoundSql(parseSql, parameterMappings);
        return boundSql;

    }


}
